<?php
////////////////////////////////////////Don't Remove this/////////////////////////////
// Project : Code Phim																//
// Version : Blog Yam 1.0 (Beta)													//
// Code by : Tiểu Tinh																//
// Y!M     : Tieutinh189															//
// Class   : Upload file 1.0 (Beta)													//
// Website : Tieutinh.info															//
////////////////////////////////////////Don't Remove this/////////////////////////////
class Libraries_upload
{
	private $config = array();
	private $error 	= array();
	private $upload;
	function __construct($config = '')
	{
		//Intial Config
		$this->config["folder"] = $config["folder"]; //Folder upload
		$this->config["name"] 	= $config["name"]; //Name $_FILE ,ten input file
		$this->config["type"] 	= $config["type"]; //"jpg|png|gif";
		$this->config["size"]	= $config["size"]; //Kich thuoc
	}
	//* Kiem tra co upload khong
	function upload_check()
	{
		if( !@is_uploaded_file( $_FILES[$this->config['name']]["tmp_name"] ) )
		{
			return false;	
		}
		else
		{
			return true;
		}
	}
	//*** Kiem tra co upload khong
	
	//* Kiem tra co upload khong
	private function upload_check2()
	{
		if( !@is_uploaded_file( $this->upload["tmp_name"] ) )
		{
			return false;	
		}
		else
		{
			return true;
		}
	}
	//*** Kiem tra co upload khong
	
	//* Lay thong bao loi
	function showerror()
	{
		$error = "";
		foreach($this->error as $value)
		{
			$error .= $value."<br />";
		}
		return $error;
	}
	//*** Lay thong bao loi
	
	//* Check co loi hay ko
	function checkerror()
	{
		$error = "";
		if(count($this->error) == 0)
		{
			return true;
		}
		else
		{
			return false;	
		}
	}
	//*** Check co loi hay ko
	
	
	//* Lay thong tin File neu da Upload thanh dong
	function infofile()
	{
		if(count($this->error) == 0)
		{
			return $this->upload;
		}
		else
		{
			return "";
		}
		
	}
	//*** Lay thong tin File neu da Upload thanh dong
	
	//* Function xu li upload
	function upload_file($file)
	{
		
		$this->upload = $file;
		//Kiem tra ten name Input cua upload co dung hay ko
		if( !$this->upload_check2() )
		{
			return false;	
		}
		
		//Gan gia tri Upload vao bien $upload
		
		
		//Xử lý tên File
		$file["name"] = $this->upload["name"];
		
		//***************************************
		//Begin Kiem tra Upload co hop le
		//***************************************
		//Kiem tra kich thuoc File
		if($this->upload["size"] > ($this->config["size"] * 1024))
		{
			$this->error[] = "File kích thước quá lớn, Chỉ cho phép file nhỏ hơn ".$this->config["size"]."kb";
			return false;
		}
		//Upload khong duoc co ky tu la
		if(!ereg("^[a-zA-Z0-9\.\_\-]*$",$file["name"]))
		{
			$this->error[] = "Tên file không hợp lệ";
			return false;
		}
		//Upload khong dc co hon 2 dau .
		$arr = explode(".",$file["name"] );
		if( count( $arr ) > 2)
		{
			$this->error[] = "Tên file không được có hơn 1 dấu .";
			return false;
		}
		//Kiem tra duoi file mo rong
		if( strripos($this->config["type"],$arr[1]) === FALSE )
		{
			$this->error[] = "Không cho phép đuôi file mở rộng là .".$arr[1];
			return false;
		}
		//***************************************
		//End Kiem tra Upload co hop le
		//***************************************
		
		//Kiem tra xem File da ton tai chua
		//Neu da ton tai se them so vao phia sau
		$i = 1;
		while(true)
		{
			if(@file_exists($this->config["folder"]."/".$file["name"]))
			{
				 $arr = explode(".",$this->upload["name"]);
				 $file["name"] = $arr[0]."_".$i.".".$arr[1];
			}
			else
			{
				$this->upload["name"] = $file["name"];
				break;	
			}
			$i++;
		}
		//Thuc hien Upload
		if( @move_uploaded_file($this->upload["tmp_name"],$this->config["folder"]."/".$this->upload["name"]) )
		{
			$slash = DIRECTORY_SEPARATOR;
			$this->upload["name_full"] = $this->config["folder"]."/".$this->upload["name"];
			$this->upload["path"]	   = dirname(__FILE__).$slash.$this->config["folder"].$slash.$this->upload["name"];
			return true;
		}
		else
		{
			$this->error[] =  "Lỗi Upload";
			return false;	
		}

	}
	//*** Function xu li upload
	
	//* Thuc hien Upload 1 file
	function do_upload()
	{
		//Kiem tra xem da truyen gia tri $config chua
		if($this->config == "")
		{
			$this->error[] = "Chưa khởi tạo Config";
			return false;
		}
		
		//Kiem tra ten name Input cua upload co dung hay ko
		if( !@is_uploaded_file($_FILES[$this->config["name"]]["tmp_name"]) )
		{
			$this->error[] = "Lỗi không đúng tên Input";
			return true;	
		}
		return $this->upload_file($_FILES[$this->config["name"]]);
	}
	//*** Thuc hien Upload 1 file
	
	//* Thuc hien upload multi file
	function do_upload_multi()
	{
		$check = true;
		//Kiem tra xem da truyen gia tri $config chua
		if($this->config == "")
		{
			$this->error[] = "Chưa khởi tạo Config";
		}

		$Ufiles = $_FILES[$this->config["name"]];
		
		foreach($Ufiles['name'] as $k => $files)
		{
			$data[$k]['name'] 		= $Ufiles['name'][$k];
			$data[$k]['type'] 		= $Ufiles['type'][$k];
			$data[$k]['tmp_name'] 	= $Ufiles['tmp_name'][$k];
			$data[$k]['error'] 		= $Ufiles['error'][$k];
			$data[$k]['size'] 		= $Ufiles['size'][$k];
			
			
			if($this->upload_file($data[$k]))
			{
				$images[] = $this->upload;
			}
		}
		$this->upload = $images;
	}
	//*** Thuc hien upload multi file
}
?>

